package com.cheyun.netsalev3.view.choosecar

import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.FragmentChooseCarFragment2Binding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.viewmodel.ChooseCarViewModel

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 */
class ChooseCarFragment2 : BaseFragment<FragmentChooseCarFragment2Binding>() {

    //车系1车型2
    private var pos: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            pos = it.getString("pos")
        }
    }

    override fun initContentView(): Int {
        return R.layout.fragment_choose_car_fragment2
    }

    override fun initViewModel(): ViewModel? {
        this?.activity?.apply {
            return ViewModelProviders.of(this).get(ChooseCarViewModel::class.java)
        }
        return null
    }

    private lateinit var adapter: MyAdapter

    override fun initView() {
        adapter = when (pos) {
            "1" -> {
                binding.titletext.text = "请选择车系"
                //车系
                MyAdapter(R.layout.vehicle_type_item, binding.viewModel)
            }
            "2" -> {
                binding.titletext.text = "请选择车型"
                //车型
                MyAdapter(R.layout.vehicle_type_item2, binding.viewModel)
            }
            else -> {
                MyAdapter(R.layout.vehicle_type_item, binding.viewModel)
            }
        }
        binding.recyclerView.adapter = adapter
    }

    override fun initData() {
        when (pos) {
            "1" -> {
                //车系
                binding.viewModel?.cexiList?.observe(this, Observer { list ->
                    adapter.submitList(list)
                })
            }
            "2" -> {
                //车型

                binding.viewModel?.carXingList?.observe(this, Observer { list ->
                    adapter.submitList(list)
                })

            }
        }
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
    }

    companion object {
        @JvmStatic
        fun newInstance(pos: String) =
                ChooseCarFragment2().apply {
                    arguments = Bundle().apply {
                        putString("pos", pos)
                    }
                }
    }
}
